# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
触发中移动项目的接口
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import os
import time
import requests
from pycacho.cachobase.file_deal import get_line_out
from pycacho.cachobase.deal_time import minus_date
from pycacho.cachobase.get_base_info import process_msydonline
from pythonlession.recordmodule.paramiko_model import SftpDeal
from pycacho.cachobase.file_rename import copy_rename_file
from msydwork.api.api_msydonline import loan_apply,package_notify,unfreeze,headers
from msydwork.api.api_batch import exec_fileLog,read_file,exec_batch
from msydwork.config.config_msydonline import ip,c_url,sftp_user,sftp_password
from msydwork.data.mysql_msydonline import get_file,get_org,delete_table,drop_table,white_user,truncate_table,update_msydloan

folder = '/data/sftp/onlinemall/home/upload/hbjd/send/'

def flow_pre():
    print('先进行数据库清库处理....')
    delete_table()
    truncate_table()
    drop_table()


# 通过文件进行正式风控规则
def file_apply():
    info = get_line_out('..\\test\\test-credit-online.txt','#')
    for data in info:
        brwOrdNo = data.split('|')[0]
        mobileNo = data.split('|')[1]
        idCardUserName = data.split('|')[2]
        idCardNo = data.split('|')[3]
        frontIdCardID = data.split('|')[4]
        blackIdCardID = data.split('|')[5]
        liveImageID = data.split('|')[6]
        bankCardNo = data.split('|')[7]
        bankMobileNo = data.split('|')[8]
        print(brwOrdNo, mobileNo, idCardUserName, idCardNo, frontIdCardID,blackIdCardID, liveImageID,bankCardNo,bankMobileNo)
        loan_apply(brwOrdNo, mobileNo, idCardUserName, idCardNo, frontIdCardID,blackIdCardID, liveImageID,bankCardNo,bankMobileNo)


def flow():
    sftp = SftpDeal(ip, sftp_user, sftp_password)
    apply_date = minus_date(1, 's')
    img_path = '/home/upload/hbjd/img/'
    pdf_path = folder + minus_date(0, 's')+'/'
    local_path = 'E:\zenglingwei\\test\pdf\\'
    info = process_msydonline(20)
    for i in range(1, 2):
        brwOrdNo = info[i][0]
        for j in range(1,4):
            local_file = local_path+'sample_'+str(j)+'.PDF'
            new_name = brwOrdNo + '_cpchOLZmkj'+str(j)+'.PDF'
            copy_rename_file(local_file,local_path,new_name)
            sftp.upload(local_file,pdf_path+new_name)
        mobileNo = info[i][1]
        idCardUserName = info[i][2]
        idCardNo = info[i][3]
        frontIdCardID = img_path + apply_date + '/' + '200508550009700443.jpg'
        blackIdCardID = img_path + apply_date + '/' + '200508550009700444.jpg'
        liveImageID = img_path + apply_date + '/' + '1906125500026459562.jpg'
        bankCardNo = '6214830116281592'
        bankMobileNo = '15210081274'
        provinceBillDate = str(i)
        print('走白名单规则，直接跳过风控规则....')
        remark = idCardUserName + ' 的身份证号'
        white_user(idCardNo,remark)
        loan_apply(brwOrdNo, mobileNo, idCardUserName, idCardNo, frontIdCardID, blackIdCardID,liveImageID,bankCardNo,bankMobileNo,provinceBillDate)
        time.sleep(5)
        orgOrderNo = get_org(brwOrdNo)
        time.sleep(2)
        package_notify(brwOrdNo, orgOrderNo)
        # 解冻
        if i ==10:
            unfreeze(brwOrdNo, apply_date)


def deal_file(file_type):
    file_date = cur_date   # minus_date(2, 's')  '20200623'
    if file_type == '放款':
        file = 'F:\888005000000002_CAPITAL_TRANS_ORDER_20201026_001.txt'      # 'F:\888005000000002_CAPITAL_TRANS_ORDER_20200623_001.txt'   get_file('01',file_date)
    else:
        file = get_file('02', file_date) #      'F:\\BACK_888005000000002_BATCH_DETAILS_20200801_001.txt'
    print(file)
    sftp = SftpDeal(ip,sftp_user,sftp_password)
    remote_txt = folder + file_date + '/' + os.path.basename(file)
    older_txt = remote_txt + '.old'
    sftp.upload(file, remote_txt)             # 上传文件到sftp，进行文件加密处理，加密后源文件重命名+.old
    fur_url = c_url + '/test/fileEncrypt?filePath=' + folder + file_date + '/&fileName=' +  os.path.basename(file)
    print('加密请求url为：',fur_url)
    requests.get(fur_url, headers=headers)
    #data = req1.text
    time.sleep(2)
    sftp.remove(older_txt)                   # 删除原始.old文件


def batch():
    exec_fileLog(c_url, 'msydonline',cur_date)        # 文件定时任务记录
    time.sleep(2)
    read_file(c_url, 'msydonline',cur_date)           # 批量扣款回盘文件、退款资金结算明细文件、业务办理成功/退货明细文件、主动还款结果明细文件


def batch_file():
    sftp = SftpDeal(ip, sftp_user, sftp_password)
    pdf_path = folder + minus_date(1, 's')+'/'
    local_path = 'E:\zenglingwei\\test\pdf\\'

    for j in range(1, 4):
        local_file = local_path + 'sample_' + str(j) + '.PDF'
        new_name = '50620200922145619667733192' + '_cpchOLZmkj' + str(j) + '.PDF'
        copy_rename_file(local_file, local_path, new_name)
        sftp.upload(local_file, pdf_path + new_name)

if __name__ == '__main__':
    i = 0
    if i == 0:    # 初始化数据、借款、办理成功通知、放款、批处理
        b_date = minus_date(0, 't')
        update_msydloan(b_date)
        cur_date = b_date.replace('-','')
        flow_pre()
        flow()
        print('>>>>>>>>>准备1.5分钟重启cronjob.....')
        time.sleep(90)
        deal_file('放款')          # 放款
        batch()

    elif i == 1:
        b_date = '2020-08-01'  #
        update_msydloan(b_date)
        exec_batch(c_url)
        time.sleep(5)
        cur_date = b_date.replace('-','')
        deal_file('回盘')               # 回盘
        batch()                   # 批处理
    elif i == 2:
        flow_pre()
        file_apply()
    elif i == 3:       # 走白名单
        flow_pre()
        flow()
    elif i == 4:
        batch_file()
    else:
        b_date = minus_date(-1, 't')
        cur_date = b_date.replace('-','')
        deal_file('放款')  # 放款
        batch()